﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.BOS;
using Kingdee.BOS.App;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Util;
using Kingdee.K3.FIN.AP.App.Report;

namespace RK.ReportPlugin.ZhongRun
{
    [Description("中润：应收往来明细表"), HotUpdate]
    public class GoAndComReport : GoAndComReportService_New
    {
        private String[] customRptTempTableNames;
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            var isMat = Convert.ToBoolean(filter.FilterParameter.CustomFilter["FShowByMaterial"]);
            IDBService dBService = ServiceHelper.GetService<IDBService>();
            customRptTempTableNames = dBService.CreateTemporaryTableName(this.Context, 1);
            String strTable = customRptTempTableNames[0];
            if (isMat)
            {
                base.BuilderReportSqlAndTempTable(filter, strTable);
                //对初步的查询结果进行处理，然后写回基类默认的存放查询结果的临时表
                StringBuilder sb = new StringBuilder();
                string strSql = "/*dialect*/select t1.*, a2.F_YEZHU into {0}  FROM {1} t1 left join  ( SELECT T1.FENTRYID,T2.FDATAVALUE F_YEZHU FROM t_AR_receivable T0 JOIN t_AR_receivableEntry T1 ON T1.FID=T0.FID LEFT JOIN T_BAS_ASSISTANTDATAENTRY_L T2 ON T2.FENTRYID=T0.FYZ AND T2.FLOCALEID=2052 )  a2 on"
                               + "  t1.FENTRYID = a2.FENTRYID ";
                //sql拼接注解：T_BD_CUSTOMER_L  客户名称表  FSHORTNAME  客户简称字段                                                                          
                //这里可以断点调试去找这两张表单的关联关系
                sb.AppendFormat(strSql, tableName, strTable);
                DBUtils.Execute(this.Context, sb.ToString());
            }
            else
            {
                //base.BuilderReportSqlAndTempTable(filter, tableName);
                base.BuilderReportSqlAndTempTable(filter, strTable);
                //对初步的查询结果进行处理，然后写回基类默认的存放查询结果的临时表
                StringBuilder sb = new StringBuilder();
                string strSql = "/*dialect*/select t1.*, a2.F_YEZHU into {0}  FROM {1} t1 left join  ( SELECT T0.FBILLNO,T2.FDATAVALUE F_YEZHU FROM t_AR_receivable T0 LEFT JOIN T_BAS_ASSISTANTDATAENTRY_L T2 ON T2.FENTRYID = T0.FYZ AND T2.FLOCALEID = 2052 )  a2 on"
                               + "  t1.FBILLNO = a2.FBILLNO ";
                //sql拼接注解：T_BD_CUSTOMER_L  客户名称表  FSHORTNAME  客户简称字段                                                                          
                //这里可以断点调试去找这两张表单的关联关系
                sb.AppendFormat(strSql, tableName, strTable);
                DBUtils.Execute(this.Context, sb.ToString());
            }
        }
        //public override ReportHeader GetReportHeaders(IRptParams filter)
        //{
        //    ReportHeader header = base.GetReportHeaders(filter);
        //    // TODO: 添加新增字段
        //    header.AddChild("F_YEZHU", new LocaleValue("业主"));
        //    return header;
        //}
        public override void CloseReport()
        {
            //删除临时表
            if (customRptTempTableNames.IsNullOrEmptyOrWhiteSpace())
            {
                return;
            }
            IDBService dbService = ServiceHelper.GetService<IDBService>();
            dbService.DeleteTemporaryTableName(this.Context, customRptTempTableNames);
            base.CloseReport();
        }
    }
}
